<?php

namespace app\company\controller\statistics\consult;

use app\BaseController;
use app\model\ConsultOfflineModel;
use app\model\ConsultOnlineModel;
use app\model\ConsultQuestionModel;
use app\model\DepartmentModel;
use app\model\UnitModel;
use app\model\UserModel;
use think\App;

class Index extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 本月咨询完结人数
     * @return bool|string
     */
    public function MonthOnlineEndNum(): bool|string
    {
        try {
            $y_start_time = strtotime(date('Y-m-01'));
            $y_end_time = strtotime(date('Y-m-t 23:59:59'));
            $where = [];
            $where[] = ['end_time','between',[$y_start_time,$y_end_time]];
            $where[] = ['is_end','=',1];
            $where[] = ['unit_id','in',$this->userData['subset_unit']];
            $ConsultOnlineModel = new ConsultOnlineModel();
            //本月咨询完结人数
            $m_end_num = $ConsultOnlineModel->where($where)->distinct(true)->count('uid');
            //历史咨询完结人数
            $end_num = $ConsultOnlineModel->where([
                ['unit_id','in',$this->userData['subset_unit']],
                ['is_end','=',1]
            ])->distinct(true)->count('uid');
            //本月占比
            $m_ratio = $end_num?round($m_end_num/$end_num*100,1):0;
            return success([
                'm_ratio' => $m_ratio,
                'm_end_num' => $m_end_num,
                'end_num' => $end_num,
            ]);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 本月预约完结人数
     * @return bool|string
     */
    public function MonthOfflineEndNum(): bool|string
    {
        try {
            $y_start_time = strtotime(date('Y-m-01'));
            $y_end_time = strtotime(date('Y-m-t 23:59:59'));
            $ConsultOfflineModel = new ConsultOfflineModel();
            //本月完结人数
            $m_end_num = $ConsultOfflineModel->where([
                ['end_time','between',[$y_start_time,$y_end_time]],
                ['is_end','=',1],
                ['unit_id','in',$this->userData['subset_unit']]
            ])->distinct(true)->count('uid');
            //历史完结人数
            $end_num = $ConsultOfflineModel->where([
                ['is_end','=',1],
                ['unit_id','in',$this->userData['subset_unit']]
            ])->distinct(true)->count('uid');
            //本月占比
            $m_ratio = $end_num?round($m_end_num/$end_num*100,1):0;
            return success([
                'm_ratio' => $m_ratio,
                'm_end_num' => $m_end_num,
                'end_num' => $end_num,
            ]);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }


    /**
     * 全年人数走势图
     * @return bool|string
     */
    public function YearTrend(): bool|string
    {
        try {
            $data = [];
            $ConsultOnlineModel = new ConsultOnlineModel();
            $ConsultOfflineModel = new ConsultOfflineModel();
            for ($i=1;$i<=12;$i++){
                $start_time = strtotime(date('Y-'.$i.'-01'));
                $end_time = strtotime(date('Y-'.$i.'-t 23:59:59'));
                $data['date'][] = $i.'月';
                //咨询
                $data['zx_num'][] = $ConsultOnlineModel->where([
                    ['time','between',[$start_time,$end_time]],
                    ['unit_id','in',$this->userData['subset_unit']]
                ])->distinct(true)->count('uid');
                //预约
                $data['yy_num'][] = $ConsultOfflineModel->where([
                    ['time','between',[$start_time,$end_time]],
                    ['unit_id','in',$this->userData['subset_unit']]
                ])->distinct(true)->count('uid');
            }
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 咨询/预约次数
     * @return bool|string
     */
    public function num(): bool|string
    {
        try {
            $ConsultOnlineModel = new ConsultOnlineModel();
            $ConsultOfflineModel = new ConsultOfflineModel();
            //咨询
            $data['online'] = $ConsultOnlineModel->where([
                ['unit_id','in',$this->userData['subset_unit']],
                ['is_end','=',0],
            ])->count('id');
            //预约
            $data['offline'] = $ConsultOfflineModel->where([
                ['unit_id','in',$this->userData['subset_unit']],
                ['is_end','=',0],
            ])->count('id');
            //咨询-完结
            $data['online_end'] = $ConsultOnlineModel->where([
                ['unit_id','in',$this->userData['subset_unit']],
                ['is_end','=',1],
            ])->count('id');
            //预约-完结
            $data['offline_end'] = $ConsultOfflineModel->where([
                ['unit_id','in',$this->userData['subset_unit']],
                ['is_end','=',1],
            ])->count('id');
            //总数
            $data['total'] = $data['online']+$data['offline']+$data['online_end']+$data['offline_end'];
            //咨询占比
            $data['online_ratio'] = $data['online']?round($data['online']/$data['total']*100,1):0;
            //预约占比
            $data['offline_ratio'] = $data['offline']?round($data['offline']/$data['total']*100,1):0;
            //咨询-完结占比
            $data['online_end_ratio'] = $data['online_end']?round($data['online_end']/$data['total']*100,1):0;
            //预约-完结占比
            $data['offline_end_ratio'] = $data['offline_end']?round($data['offline_end']/$data['total']*100,1):0;
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 咨询服务覆盖人群统计
     * @return bool|string
     */
    public function crowd(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'pageSize|每页显示数量' => 'require',
            ]);
            $ConsultOnlineModel = new ConsultOnlineModel();
            $ConsultOfflineModel = new ConsultOfflineModel();
            $DepartmentModel = new DepartmentModel();
            $UnitModel = new UnitModel();
            $department_num = $DepartmentModel->where('unit_id','in',$this->userData['subset_unit'])->count('id');
            $online = $ConsultOnlineModel->where('unit_id','in',$this->userData['subset_unit'])->distinct(true)->count('uid');
            $offline = $ConsultOfflineModel->where('unit_id','in',$this->userData['subset_unit'])->distinct(true)->count('uid');
            $crowd = $online+$offline;
            $data = $UnitModel->field('id,name')
                ->where('id','in',$this->userData['subset_unit'])->paginate($param['pageSize'])->toArray();
            foreach ($data['data'] as &$item){
                $online = $ConsultOnlineModel->where('unit_id','=',$item['id'])->distinct(true)->count('uid');
                $offline = $ConsultOfflineModel->where('unit_id','=',$item['id'])->distinct(true)->count('uid');
                $item['num'] = $online+$offline;
                $item['ratio'] = $crowd?round($item['num']/$crowd*100,1) . '%':'0%';
            }
            $data['crowd'] = $crowd;
            $data['department_num'] = $department_num;
            $data['unit_num'] = count($this->userData['subset_unit']);
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 问题类型数据统计
     * @return bool|string
     */
    public function QuestionType(): bool|string
    {
        try {
            $ConsultOnlineModel = new ConsultOnlineModel();
            $ConsultOfflineModel = new ConsultOfflineModel();
            $ConsultQuestionModel = new ConsultQuestionModel();
            $question_info = $ConsultQuestionModel->where('status','=',1)->find();
            $question_id = $question_info['question_id'];
            $data['title'] = $question_info['name'];
            for ($i=1;$i<=12;$i++){
                $start_time = strtotime(date('Y-'.$i.'-01'));
                $end_time = strtotime(date('Y-'.$i.'-t 23:59:59'));
                $data['trend_date'][] = $i.'月';
                $offline = $ConsultOfflineModel->where([
                    ['unit_id','in',$this->userData['subset_unit']],
                    ['question_id','=',$question_id],
                    ['time','between',[$start_time,$end_time]]
                ])->distinct()->count('uid');
                $online = $ConsultOnlineModel->where([
                    ['unit_id','in',$this->userData['subset_unit']],
                    ['question_id','=',$question_id],
                    ['time','between',[$start_time,$end_time]]
                ])->distinct()->count('uid');
                $data['trend_num'][] = $offline+$online;
            }
            $question = $ConsultQuestionModel->field('id,name')->where([
                ['unit_id','in',$this->userData['subset_unit']],
                ['status','=',1]
            ])->select()->toArray();
            $list = [];
            foreach ($question as &$item) {
                $offline = $ConsultOfflineModel->where('question_id','=',$item['id'])->distinct()->count('uid');
                $online = $ConsultOnlineModel->where('question_id','=',$item['id'])->distinct()->count('uid');
                $item['num'] = $offline+$online;
                $list[] = $item;
            }
            if (!empty($list)) {
                $field = array_column($list,'num');
                // 假设按’field’字段升序排序
                array_multisort($field, SORT_DESC, $list);
            }
            $data['list'] = $list;
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

}